package processer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.LianJiaUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;
import java.util.Random;

public class LianJiaProcesser implements PageProcessor {

    private static Logger logger = LoggerFactory.getLogger(LianJiaProcesser.class);

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

    private int currentPage = 0;

    private Random random = new Random();

    private String searchv5UrlPattern = "https://app.api.lianjia.com/house/ershoufang/searchv5?city_id=440100&is_history=0&full_filters=0&limit_offset=${limitOffset}&has_recommend=0&is_suggestion=0&isFromMap=false&ad_recommend=1&limit_count=20";
    private String detailpart1v1UrlPattern = "https://app.api.lianjia.com/house/ershoufang/detailpart1v1?house_code=${houseCode}&fb_expo_id=&is_hot=0";



    @Override
    public void process(Page page) {
        if(StringUtils.equals("https://app.api.lianjia.com/house/ershoufang/searchv5",page.getUrl().toString().split("\\?")[0])) {
            JSONArray houseList = JSONObject.parseObject(page.getJson().toString()).getJSONObject("data").getJSONArray("list");
            List<Request> houseDetailRequestList = Lists.newArrayList();
            houseList.stream().forEach(object -> {
                JSONObject jsonObject = (JSONObject) object;
                if (jsonObject.containsKey("house_code")) {
                    houseDetailRequestList.add(LianJiaUtil.createGetMethodRequest(detailpart1v1UrlPattern.replace("${houseCode}",jsonObject.getString("house_code"))));
                }
            });
            int sleepSecond = random.nextInt(11)+10;//随机睡眠10~20秒
            sleep(sleepSecond);
            houseDetailRequestList.stream().forEach(request -> page.addTargetRequest(request));
            //判断是否还有下一页
            if(1 == JSONObject.parseObject(page.getJson().toString()).getJSONObject("data").getIntValue("has_more_data")){
                currentPage++;
                String nextPageUrl = searchv5UrlPattern.replace("${limitOffset}",currentPage*20+"");
                page.addTargetRequest(LianJiaUtil.createGetMethodRequest(nextPageUrl));
            }
            page.setSkip(true);
        }else if(StringUtils.equals("https://app.api.lianjia.com/house/ershoufang/detailpart1v1",page.getUrl().toString().split("\\?")[0])){
            int sleepSecond = random.nextInt(4)+2;//随机睡眠2~5秒
            sleep(sleepSecond);
            JSONObject responseJson = JSONObject.parseObject(page.getJson().toString());
            JSONObject basicInfo = responseJson.getJSONObject("data").getJSONObject("basic_info");
            JSONObject result = new JSONObject();
            result.put("house_code",basicInfo.getString("house_code"));
            result.put("m_url",basicInfo.getString("m_url"));//可在网页直接打开看详情
            result.put("title",basicInfo.getString("title"));
            result.put("price",basicInfo.getString("price"));//总价 单位：元
            result.put("unit_price",basicInfo.getString("unit_price"));//单价 单位：元
            result.put("blueprint_hall_num",basicInfo.getString("blueprint_hall_num"));//大厅数量
            result.put("blueprint_bedroom_num",basicInfo.getString("blueprint_bedroom_num"));//房间数量
            result.put("area",basicInfo.getString("area"));//面积 单位 平方米
            result.put("floor_state",basicInfo.getString("floor_state"));//楼层
            result.put("community_name",basicInfo.getString("community_name"));//楼盘名称
            result.put("community_id",basicInfo.getString("community_id"));//楼盘id
            result.put("orientation",basicInfo.getString("orientation"));//朝向
            result.put("publish_time",responseJson.getJSONObject("data").getJSONArray("info_list").getJSONObject(1).getString("value"));//发布时间
            result.put("age",responseJson.getJSONObject("data").getJSONArray("info_list").getJSONObject(7).getString("value"));//建成时间
            result.put("house_type",responseJson.getJSONObject("data").getJSONArray("info_list").getJSONObject(8).getString("value"));//类型
            result.put("legal_type",responseJson.getJSONObject("data").getJSONArray("info_list").getJSONObject(9).getString("value"));//建成时间
            result.put("sf_total",responseJson.getJSONObject("data").getJSONArray("info_jump_list").getJSONObject(2).getString("sfTotal"));//首付
            result.put("monthly_supply",responseJson.getJSONObject("data").getJSONArray("info_jump_list").getJSONObject(2).getString("monthlySupply"));//月供
            String location = responseJson.getJSONObject("data").getJSONObject("location").getString("title");
            String []locationArr = location.split("，");
            result.put("district_name",locationArr[0]);//区域
            result.put("bizcircle_name",locationArr[1]);//子区域
            result.put("request_id",responseJson.getString("request_id"));
            result.put("crawl_time",System.currentTimeMillis());
            page.putField("row",result);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    private void sleep(int second){
        try {
            logger.info("睡眠{}秒钟。。。",second);
            Thread.sleep(second*1000);
            logger.info("睡醒继续干活！！！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
